﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace SIMSProject.Helpers
{
    class ListHelper
    {
        private readonly List<BaseFieldCreator> creators;

        private readonly SPWeb web;

        private string archivedListName;

        public ListHelper(SPWeb web)
        {
            this.web = web;
            creators = new List<BaseFieldCreator>();
            ListTemplateType = SPListTemplateType.GenericList;
            Views = new List<ViewCreator>();
        }

        public string Title { get; set; }

        public string Description { get; set; }

        public SPListTemplateType ListTemplateType { get; set; }

        public bool OnQuickLaunch { get; set; }

        /// <summary>
        /// Indicates whether throttling for this list is disable.
        /// </summary>
        public bool DisableListThrottling { get; set; }

        /// <summary>
        /// Indicates whether throttling for this archive list is disable.
        /// </summary>
        public bool DisableArchiveListThrottling { get; set; }

        /// <summary>
        ///   Enabled archives for this list
        /// </summary>
        public bool CreateArchivedList { get; set; }

        /// <summary>
        ///   Archive list name, default is "Archived [List Name]"
        /// </summary>
        public string ArchivedListName
        {
            get
            {
                if (string.IsNullOrEmpty(archivedListName))
                {
                    return "Archived " + Title;
                }
                return archivedListName;
            }
            set { archivedListName = value; }
        }

        public bool ShowArchivedListOnQuickLaunch { get; set; }

        /// <summary>
        /// Gets or sets a Boolean value that specifies whether attachments can be added to items in the list.
        /// </summary>
        public bool EnableAttachments { get; set; }

        public IList<ViewCreator> Views { get; private set; }

        /// <summary>
        /// Create a list with fields
        /// </summary>
        /// <returns></returns>
        public SPList Apply()
        {
            var list = CreateList(Title, Description, ListTemplateType, OnQuickLaunch, DisableListThrottling);

            if (!EnableAttachments)
            {
                list.EnableAttachments = false;
            }

            list.Update();

            // Create views
            foreach (var viewCreator in Views)
            {
                viewCreator.Apply(list);
            }

            return list;
        }

        private SPList CreateList(string title, string description, SPListTemplateType listTemplateType, bool onQuickLaunch, bool disableListThrottling)
        {
            var list = web.Lists.TryGetList(title);

            if (list == null)
            {
                var id = web.Lists.Add(title, description, listTemplateType);
                list = web.Lists[id];

                try
                {
                    list.OnQuickLaunch = onQuickLaunch;

                    foreach (var creator in creators)
                    {
                        creator.CreateField(list);
                    }

                    if (disableListThrottling)
                    {
                        list.EnableThrottling = false;
                    }

                    list.Update();

                    return list;
                }
                catch (Exception ex)
                {
                    if (id != Guid.Empty)
                    {
                        web.Lists[id].Delete();
                    }

                    throw new ArgumentException(
                        string.Format("Cannot create a list '{0}' because: {1}", title, ex.Message), ex);
                }
            }

            return list;
        }

        public void AddField(BaseFieldCreator creator)
        {
            creators.Add(creator);
        }

        /// <summary>
        /// Indicates whether throttling for this list is disable.
        /// </summary>
        public static void DisableListThreshold(SPList list)
        {
            list.EnableThrottling = false;
            list.Update();
        }
    }
}
